home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / FLTK-1.0.6 / src / Fl_Button.cxx < prev    next >
Encoding:
C/C++ Source or Header  |  1999-01-07  |  3.5 KB  |  120 lines

  1. //
  2. // "$Id: Fl_Button.cxx,v 1.4 1999/01/07 19:17:17 mike Exp $"
  3. //
  4. // Button widget for the Fast Light Tool Kit (FLTK).
  5. //
  6. // Copyright 1998-1999 by Bill Spitzak and others.
  7. //
  8. // This library is free software; you can redistribute it and/or
  9. // modify it under the terms of the GNU Library General Public
  10. // License as published by the Free Software Foundation; either
  11. // version 2 of the License, or (at your option) any later version.
  12. //
  13. // This library is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. // Library General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Library General Public
  19. // License along with this library; if not, write to the Free Software
  20. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  21. // USA.
  22. //
  23. // Please report all bugs and problems to "fltk-bugs@easysw.com".
  24. //
  25.  
  26. #include <FL/Fl.H>
  27. #include <FL/Fl_Button.H>
  28. #include <FL/Fl_Group.H>
  29.  
  30. // There are a lot of subclasses, named Fl_*_Button.  Some of
  31. // them are implemented by setting the type() value and testing it
  32. // here.  This includes Fl_Radio_Button and Fl_Toggle_Button
  33.  
  34. int Fl_Button::value(int v) {
  35.   v = v ? 1 : 0;
  36.   oldval = v;
  37.   clear_changed();
  38.   if (value_ != v) {value_ = v; redraw(); return 1;}
  39.   else return 0;
  40. }
  41.  
  42. void Fl_Button::setonly() { // set this radio button on, turn others off
  43.   value(1);
  44.   Fl_Group* g = (Fl_Group*)parent();
  45.   Fl_Widget*const* a = g->array();
  46.   for (int i = g->children(); i--;) {
  47.     Fl_Widget* o = *a++;
  48.     if (o != this && o->type()==FL_RADIO_BUTTON) ((Fl_Button*)o)->value(0);
  49.   }
  50. }
  51.  
  52. void Fl_Button::draw() {
  53.   if (type() == FL_HIDDEN_BUTTON) return;
  54.   Fl_Color col = value() ? selection_color() : color();
  55. //if (col == FL_GRAY && Fl::belowmouse()==this) col = FL_LIGHT1;
  56.   draw_box(value() ? (down_box()?down_box():down(box())) : box(), col);
  57.   draw_label();
  58. }
  59. int Fl_Button::handle(int event) {
  60.   int newval;
  61.   switch (event) {
  62.   case FL_ENTER:
  63.   case FL_LEAVE:
  64. //  if ((value_?selection_color():color())==FL_GRAY) redraw();
  65.     return 1;
  66.   case FL_PUSH:
  67.   case FL_DRAG:
  68.     if (Fl::event_inside(this)) {
  69.       if (type() == FL_RADIO_BUTTON) newval = 1;
  70.       else newval = !oldval;
  71.     } else
  72.       newval = oldval;
  73.     if (newval != value_) {
  74.       value_ = newval;
  75.       redraw();
  76.       if (when() & FL_WHEN_CHANGED) do_callback();
  77.     }
  78.     return 1;
  79.   case FL_RELEASE:
  80.     if (value_ == oldval) return 1;
  81.     if (type() == FL_RADIO_BUTTON)
  82.       setonly();
  83.     else if (type() == FL_TOGGLE_BUTTON)
  84.       oldval = value_;
  85.     else {
  86.       value(oldval);
  87.       if (when() & FL_WHEN_CHANGED) do_callback();
  88.     }
  89.     if (when() & FL_WHEN_RELEASE) do_callback(); else set_changed();
  90.     return 1;
  91.   case FL_SHORTCUT:
  92.     if (!(shortcut() ?
  93.       Fl::test_shortcut(shortcut()) : test_shortcut())) return 0;
  94.     if (type() == FL_RADIO_BUTTON && !value_) {
  95.       setonly();
  96.       if (when() & FL_WHEN_CHANGED) do_callback();
  97.     } else if (type() == FL_TOGGLE_BUTTON) {
  98.       value(!value());
  99.       if (when() & FL_WHEN_CHANGED) do_callback();
  100.     }
  101.     if (when() & FL_WHEN_RELEASE) do_callback(); else set_changed();
  102.     return 1;
  103.   default:
  104.     return 0;
  105.   }
  106. }
  107.  
  108. Fl_Button::Fl_Button(int x,int y,int w,int h, const char *l)
  109. : Fl_Widget(x,y,w,h,l) {
  110.   box(FL_UP_BOX);
  111.   down_box(FL_NO_BOX);
  112.   value_ = oldval = 0;
  113.   shortcut_ = 0;
  114.   set_flag(SHORTCUT_LABEL);
  115. }
  116.  
  117. //
  118. // End of "$Id: Fl_Button.cxx,v 1.4 1999/01/07 19:17:17 mike Exp $".
  119. //
  120.